﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Brain;
using System.Collections;
using Mighty.Common;

namespace Mighty.Brain
{
    public class PianoBrain : BasicBrain
    {
        int mylimit=13;
        bool BettingOk;

        bool amIOffense = false;
        bool amIFriend = false;
        bool amIDefense = false;

        //내가 정할 기루다와 게임에서 정해진 기루다 생성
        Giruda myGiruda = new Giruda();
        Giruda gameGiruda = new Giruda();
        Card tmpGiruda = new Card();

        Card tmpCard = new Card();
        Card tmpCard2 = new Card();
        Card tmpCard3 = new Card();
        Card tmpCard4 = new Card();

        //마이티,조커, 각 무늬 카드들의 생성
        Card mightyCard = new Card();
        Card jokerCard = new Card();

        Card SpadeCard = new Card();
        Card heartCard = new Card();
        Card cloverCard = new Card();
        Card diamondCard = new Card();
        Card girudaWincard = new Card();

        //카드들을 담아두기 위한 배열
        HandedCard[] clover;
        HandedCard[] diamond;
        HandedCard[] spade;
        HandedCard[] heart;
        HandedCard[] joker;

        int clover_count = 0;
        int diamond_count = 0;
        int spade_count = 0;
        int heart_count = 0;
        int joker_count = 0;

        int myPosition;

        FriendPlayer frien = new FriendPlayer();


        public PianoBrain()
        {
            // 초기화
            jokerCard.shape = Shape.Joker;
            SpadeCard.shape = Shape.Spade;
            heartCard.shape = Shape.Heart;
            cloverCard.shape = Shape.Clover;
            diamondCard.shape = Shape.Diamond;
        }

        public HandedCard MinCard()
        {
            HandedCard n = new HandedCard();
            ArrayList possibleCards = new ArrayList();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) possibleCards.Add(card);
            }
            n.number = ((HandedCard)possibleCards[0]).number;
            n.shape = ((HandedCard)possibleCards[0]).shape;

            for (int i = 0; i < possibleCards.Count; i++)
            {
                if (n.number > ((HandedCard)possibleCards[i]).number)
                {
                    n.number = ((HandedCard)possibleCards[i]).number;
                    n.shape = ((HandedCard)possibleCards[i]).shape;
                }
            }

            return n;
        }

        public void insertion_Sort(HandedCard[] s, int n)
        {
            //insertion_Sort(int list[], int n)
            int i,j;
            int next;
            for(i=1;i<n;i++)
            {
                next=s[i].number;
                for(j=i-1;j>=0 && next<s[j].number;j--)
                {
                    s[j+1].number=s[j].number;
                }
                s[j+1].number=next;
            }
        }

        public void UpdateCardArray()
        {
            int deckCnt = gameInfo.myCards.Length;
            clover_count = 0;
            diamond_count = 0;
            spade_count = 0;
            heart_count = 0;
            joker_count = 0;
            
            int i = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Clover && gameInfo.myCards[i].isUsed == false) clover_count++;
            }
            clover = new HandedCard[clover_count];


            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Diamond && gameInfo.myCards[i].isUsed == false) diamond_count++;
            }
            diamond = new HandedCard[diamond_count];


            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Spade && gameInfo.myCards[i].isUsed == false) spade_count++;
            }
            spade = new HandedCard[spade_count];


            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Heart && gameInfo.myCards[i].isUsed == false) heart_count++;
            }
            heart = new HandedCard[heart_count];


            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Joker && gameInfo.myCards[i].isUsed == false) joker_count++;
            }
            joker = new HandedCard[joker_count];

            int tmp = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Joker && gameInfo.myCards[i].isUsed == false)
                {
                    joker[tmp] = gameInfo.myCards[i];
                    tmp++;
                }
            }

            tmp = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Clover && gameInfo.myCards[i].isUsed == false)
                {
                    clover[tmp] = gameInfo.myCards[i];
                    tmp++;
                }
            }
            insertion_Sort(clover, clover.Length);
            
            
            tmp = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Diamond && gameInfo.myCards[i].isUsed == false)
                {
                    diamond[tmp] = gameInfo.myCards[i];
                    tmp++;
                }

            }
            insertion_Sort(diamond,diamond.Length);

            tmp = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Heart && gameInfo.myCards[i].isUsed == false)
                {
                    heart[tmp] = gameInfo.myCards[i];
                    tmp++;
                }
            }
            insertion_Sort(heart, heart.Length);

            tmp = 0;
            for (i = 0; i < deckCnt; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Spade && gameInfo.myCards[i].isUsed == false)
                {
                    spade[tmp] = gameInfo.myCards[i];
                    tmp++;
                }
            }
            insertion_Sort(spade, spade.Length);

        }


        //해당 무늬가 있는지 알아본다
        public int FindShape(Card wantedShape)
        {
            UpdateCardArray();
            int i, cnt = 0;
            HandedCard fShape = new HandedCard();
            fShape.shape = wantedShape.shape;

            switch (fShape.shape)
            {
                case Shape.Clover:
                    for (i = 0; i < clover_count; i++)
                    {
                        if (clover[i].isUsed == false) cnt = cnt + 1;
                    }
                    break;
                case Shape.Diamond:
                    for (i = 0; i < diamond_count; i++)
                    {
                        if (diamond[i].isUsed == false) cnt = cnt + 1;
                    }
                    break;
                case Shape.Heart:
                    for (i = 0; i < heart_count; i++)
                    {
                        if (heart[i].isUsed == false) cnt = cnt + 1;
                    }
                    break;
                case Shape.Spade:
                    for (i = 0; i < spade_count; i++)
                    {
                        if (spade[i].isUsed == false) cnt = cnt + 1;
                    }
                    break;
                default:
                    break;
            }
            return cnt;
        }

        public bool FindCard(Card wantedCard)
        {
            int i;
            bool find = false;
            HandedCard fCard = new HandedCard();
            fCard.shape = wantedCard.shape;
            fCard.number = wantedCard.number;

            switch (fCard.shape)
            {
                case Shape.Clover:
                    for (i = 0; i < clover_count; i++)
                    {
                        if (clover[i].number == fCard.number && clover[i].isUsed == false) find = true;
                    }
                    break;
                case Shape.Diamond:
                    for (i = 0; i < diamond_count; i++)
                    {
                        if (diamond[i].number == fCard.number && diamond[i].isUsed == false) find = true;
                    }
                    break;
                case Shape.Heart:
                    for (i = 0; i < heart_count; i++)
                    {
                        if (heart[i].number == fCard.number && heart[i].isUsed == false) find = true;
                    }
                    break;
                case Shape.Spade:
                    for (i = 0; i < spade_count; i++)
                    {
                        if (spade[i].number == fCard.number && spade[i].isUsed == false) find = true;
                    }
                    break;
                case Shape.Joker:
                    if (joker.Length > 0 && joker[0].isUsed == false) find = true;
                    break;
                default:
                    find = false;
                    break;
            }

            return find;
        }

        public override void BettingReadyEvent()
        {
            int i, isPato;
            bool pato;

            int cp=0;
            int sp=0;
            int hp=0;
            int dp=0;

            //카드의 무늬를 판별하여 배열 생성

            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Clover) clover_count++;
            }
            clover = new HandedCard[clover_count];


            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Diamond) diamond_count++;
            }
            diamond = new HandedCard[diamond_count];


            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Spade) spade_count++;
            }
            spade = new HandedCard[spade_count];


            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Heart) heart_count++;
            }
            heart = new HandedCard[heart_count];


            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].shape == Shape.Joker) joker_count++;
            }
            joker = new HandedCard[joker_count];

            //파토인지 점검
            isPato = 0;
            for (i = 0; i < 10; i++)
            {
                if (gameInfo.myCards[i].number > 9) isPato++;
            }
            if (joker_count == 1) isPato = isPato - 1;

            //파토가 아닐경우 각 카드를 배열에 넣음
            if (isPato < 2)
            {
                pato = true;
            }
            else
            {
                pato = false;

                int tmp = 0;

                for (i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].shape == Shape.Joker)
                    {
                        joker[tmp] = gameInfo.myCards[i];
                        tmp++;
                        
                    }
                }

                tmp = 0;
                for (i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].shape == Shape.Clover)
                    {
                        clover[tmp] = gameInfo.myCards[i];
                        tmp++;
                        cp+=gameInfo.myCards[i].number;
                    }
                }

                tmp = 0;
                for (i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].shape == Shape.Diamond)
                    {
                        diamond[tmp] = gameInfo.myCards[i];
                        tmp++;
                        dp+=gameInfo.myCards[i].number;
                    }

                }

                tmp = 0;
                for (i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].shape == Shape.Heart)
                    {
                        heart[tmp] = gameInfo.myCards[i];
                        tmp++;
                        hp+=gameInfo.myCards[i].number;
                    }
                }

                tmp = 0;
                for (i = 0; i < 10; i++)
                {
                    if (gameInfo.myCards[i].shape == Shape.Spade)
                    {
                        spade[tmp] = gameInfo.myCards[i];
                        tmp++;
                        sp+=gameInfo.myCards[i].number;
                    }
                }

            }

            
            if (clover.Length >= 4&&cp>34)
            {
                myGiruda = Giruda.Clover;
                BettingOk = true;
            }

            else if (diamond.Length >= 4&&dp>34)
            {
                myGiruda = Giruda.Diamond;
                BettingOk = true;
            }
            else if (spade.Length >= 4&&sp>34)
            {
                myGiruda = Giruda.Spade;
                BettingOk = true;
            }
            else if (heart.Length >= 4&&hp>34)
            {
                myGiruda = Giruda.Heart;
                BettingOk = true;
            }
            else
            {
                myGiruda = Giruda.NoGiruda;
                BettingOk = false;
            }

            if (myGiruda == Giruda.Spade)
            {
                mightyCard.shape = Shape.Diamond;
                mightyCard.number = 14;
            }
            else
            {
                mightyCard.shape = Shape.Spade;
                mightyCard.number = 14;
            }

            jokerCard.shape = Shape.Joker;
            jokerCard.number = 0;

            if(FindCard(mightyCard))
                mylimit+=1;
            if(FindCard(jokerCard))
                mylimit+=1;
            switch (myGiruda)
            {
                case Giruda.Clover:
                    if (cp > 45) mylimit += 1;
                    break;
                case Giruda.Diamond:
                    if (dp > 45) mylimit += 1;
                    break;
                case Giruda.Heart:
                    if (hp > 45) mylimit += 1;
                    break;
                case Giruda.Spade:
                    if (sp > 45) mylimit += 1;
                    break;
            }

        }

        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;
            
            //내가 처음시작이고 베팅의지가 있는경우 기본값으로 베팅
            if (gameInfo.bettingHistory.Count == 0)
            {
                if (BettingOk == true)
                {
                    betting.isPass = false;
                    betting.giruda = myGiruda;
                    betting.limit = 13;
                }
                else
                {
                    betting.isPass = true;
                }
            }

            //이전 베팅이 있는 경우 내 베팅 limit 가 이전 베팅보다 높을 경우 베팅
            else if (gameInfo.betting.limit > 0)
            {
                //Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                Betting previousBetting = (Betting)gameInfo.betting;
                if (previousBetting.limit < mylimit && BettingOk == true) 
                {
                    betting.isPass = false;
                    betting.giruda = myGiruda;
                    betting.limit = mylimit;
                }
                else
                {
                    betting.isPass = true;
                }
            }
            else
            {
                betting.isPass = true;
            }
            return betting;
        }

        public override void BettingCompleteEvent()
        {


        }

        public override HandedCard[] ChoiceCardEvent()
        {
            int i, cnt = 0;
            int bettingHistoryCnt = 0;
            UpdateCardArray();

            //카드들을 담아두기 위한 배열
            //clover;diamond;spade;heart;joker;
            //clover_count diamond_count spade_count heart_count joker_count

            HandedCard[] myAddedCards = new HandedCard[13];

            for (i = 0; i < 13; i++)
            {
                myAddedCards[i] = gameInfo.myCards[i];
                myAddedCards[i].cmdRemove = false;
            }

            for (i = 0; i < 13; i++)
            {
                if ((myAddedCards[i].shape == gameInfo.mighty.shape && myAddedCards[i].number == gameInfo.mighty.number) ||
                    (myAddedCards[i].shape == Shape.Joker) ||
                    (myAddedCards[i].shape == (Shape)gameGiruda) ||
                    (myAddedCards[i].number >= 12))
                    {
                        //마이티,조커,기루다무늬, Q이상은 모두 가지고 있는다.
                        myAddedCards[i].cmdRemove = false;
                    }
            }

            if (clover_count <= 3 || heart_count <= 3 || spade_count <= 3 || diamond_count <= 3)
            {



                if (clover_count <= 3 && (Shape)gameGiruda != Shape.Clover)
                {
                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.shape == Shape.Clover && hc.number < 12)
                        {
                            hc.cmdRemove = true;
                        }
                    }

                }

                if (diamond_count <= 3 && (Shape)gameGiruda != Shape.Diamond)
                {
                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.shape == Shape.Diamond && hc.number < 12)
                        {
                            hc.cmdRemove = true;
                        }
                    }

                }

                if (heart_count <= 3 && (Shape)gameGiruda != Shape.Heart)
                {
                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.shape == Shape.Heart && hc.number < 12)
                        {
                            hc.cmdRemove = true;
                        }
                    }

                }

                if (spade_count <= 3 && (Shape)gameGiruda != Shape.Spade)
                {
                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.shape == Shape.Spade && hc.number < 12)
                        {
                            hc.cmdRemove = true;
                        }
                    }

                }
            }
           
            do
            {
                cnt = 0;    //주공시 버릴 카드(true) 개수 계산
                foreach (HandedCard hc in myAddedCards)
                {
                    if (hc.cmdRemove == true)
                    {
                        cnt++;
                    }
                }

                // 버릴 카드가 3장인 경우 loop 종료
                if (cnt == 3)
                {
                    break;
                }

                // 너무 많이 버렸을때
                if (cnt > 3)
                {
                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.cmdRemove==true && hc.number<10)
                        {
                            hc.cmdRemove = false;
                            break;
                        }
                    }

                    foreach (HandedCard hc in myAddedCards)
                    {
                        if (hc.cmdRemove == true && hc.number < 12)
                        {
                            hc.cmdRemove = false;
                            break;
                        }
                    }
                }

                //더버려야 할때
                else if (cnt < 3)
                {
                    bettingHistoryCnt = gameInfo.bettingHistory.Count;
                    if (bettingHistoryCnt > 5)
                    {
                        foreach (Betting b in gameInfo.bettingHistory)
                        {
                            if (b.position != myPosition && b.isPass == false && cnt < 3)
                            {
                                foreach (HandedCard hc in myAddedCards)
                                {
                                    if ((Shape)b.giruda == hc.shape && hc.shape != (Shape)myGiruda && hc.cmdRemove == false && hc.number < 12 && cnt < 3)
                                    {
                                        hc.cmdRemove = true;
                                        cnt++;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (HandedCard hc in myAddedCards)
                        {
                            if (hc.shape != (Shape)myGiruda && hc.shape != Shape.Joker && hc.cmdRemove == false && hc.number < 12 && cnt < 3)
                            {
                                hc.cmdRemove = true;
                                cnt++;
                            }
                        }

                    }
                }

            }while (cnt != 3);


            for (i = 0; i < 13; i++)
            {
                gameInfo.myCards[i]=myAddedCards[i];
            }

            return gameInfo.myCards;
        }

        public override FriendPlayer ChoiceFriendEvent()
        {
            FriendPlayer f = new FriendPlayer();
            try
            {
                gameGiruda = (Giruda)gameInfo.betting.giruda;

                mightyCard.shape = Shape.Spade;
                mightyCard.number = 14;

                if ((Shape)gameGiruda == Shape.Spade)
                {
                    mightyCard.shape = Shape.Diamond;
                    mightyCard.number = 14;
                }

                jokerCard.shape = Shape.Joker;
                jokerCard.number = 0;


                girudaWincard.shape = (Shape)gameGiruda;
                girudaWincard.number = 14;

                for (int i = 0; i < 14; i++)
                {
                    if (FindCard(girudaWincard))
                    {
                        girudaWincard.number = girudaWincard.number - 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (FindCard(mightyCard))
                {
                    if (FindCard(jokerCard))
                    {
                        f.howToSelect = FriendPlayer.HowToSelect.card;
                        /*
                            f.cardCondition.number = girudaWincard.number;
                            f.cardCondition.shape = girudaWincard.shape;
                        */
                        f.cardCondition = girudaWincard;
                    }
                    else
                    {
                        f.howToSelect = FriendPlayer.HowToSelect.card;
                        /*
                            f.cardCondition.shape = Shape.Joker;
                            f.cardCondition.number = 0;
                        */ 
                        f.cardCondition = jokerCard;
                    }
                }
                else
                {
                    //마이티 프렌드 지정
                    f.howToSelect = FriendPlayer.HowToSelect.card;
                    /*
                    //error!
                         f.cardCondition.number = mightyCard.number;
                         f.cardCondition.shape = mightyCard.shape;
                    */
                    f.cardCondition = mightyCard;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return f;
        }

        public override Betting LastBettingEvent()
        {
            Betting b = new Betting();
            b.giruda = myGiruda;
            b.isPass = BettingOk;
            b.limit = mylimit;

            return gameInfo.betting;
        }

        public override void RoundReadyEvent()
        {

            if (gameInfo.attackPlayer.name == gameInfo.myInfo.name && gameInfo.attackPlayer.position == gameInfo.myInfo.position)
            {
                amIOffense = true;
            }
            else
            {
                if (gameInfo.friend.howToSelect == FriendPlayer.HowToSelect.player && gameInfo.friend.playerCondition == gameInfo.myInfo)
                {
                    amIFriend = true;
                }
                else if (gameInfo.friend.howToSelect == FriendPlayer.HowToSelect.card && FindCard(gameInfo.friend.cardCondition))
                {
                    amIFriend = true;
                }
                else
                {
                    amIDefense = true;
                }
            }

            mightyCard.shape = gameInfo.mighty.shape;
            mightyCard.number = gameInfo.mighty.number;

            gameGiruda = gameInfo.betting.giruda;
            myPosition = gameInfo.myInfo.position;
        }

        /// <summary>
        /// 공격일 경우와 수비일 경우를 나누어
        /// 각각 함수를 호출
        /// </summary>
        public override HandedCard RoundTicEvent()
        {

            /*
             * 내가 공격이다.
                -내가 주공이다.
                -내가 프렌드이다.
               내가 수비다.
                -프렌드가 밝혀졌다.
                -프렌드가 누군지 모른다.
             * 
             */ 
            HandedCard h = new HandedCard();
            UpdateCardArray();

            //공격일때
            if(amIOffense)
            {
                h = nowAttack();
                return h;
            }
            //수비일때
            else
            {
                h = nowDefense();
                return h;
            }

        }

        public HandedCard nowAttack()
        {
            int n = 0, cn = 0;
            int lastRoundWinner = 0;
            HandedCard h = new HandedCard();
            Round lastRound = new Round();


            Card gCard = new Card();
            gCard.shape = (Shape)gameGiruda;

            Card firstCard = new Card();

            //주공일 때, 맨 처음 낼 카드
            if (gameInfo.roundIdx == 0)
            {
                HandedCard isAce = new HandedCard();
                for (int i = 0; i < 10; i++)
                {
                    if(gameInfo.myCards[i].number==14 && gameInfo.myCards[i].shape != (Shape)gameGiruda)
                    {
                            h.number = 14;
                            h.shape = gameInfo.myCards[i].shape;
                            return h;
                    }
                }

                for (int i = 0; i < 10; i++)
                {
                    if (gameGiruda == Giruda.Spade)
                    {
                        if (gameInfo.myCards[i].shape != (Shape)gameGiruda && gameInfo.myCards[i].shape != Shape.Diamond)
                        {
                            h.number = gameInfo.myCards[i].number;
                            h.shape = gameInfo.myCards[i].shape;
                            return h;
                        }

                    }

                    else
                    {
                        if (gameInfo.myCards[i].shape != (Shape)gameGiruda && gameInfo.myCards[i].shape != Shape.Spade)
                        {
                            h.number = gameInfo.myCards[i].number;
                            h.shape = gameInfo.myCards[i].shape;
                            return h;
                        }

                    }
                }
            }

            if (gameInfo.roundIdx == 1)
            {
                lastRound = gameInfo.roundHistory[gameInfo.roundIdx - 1];
                lastRoundWinner = lastRound.winner;
                if(gameInfo.myInfo.position == lastRoundWinner)
                {
                    switch(myGiruda){
                        case Giruda.Clover:
                            h.shape = Shape.Clover;
                            h.number = clover[clover.Length].number;
                            break;
                        case Giruda.Diamond:
                            h.shape = Shape.Diamond;
                            h.number = diamond[diamond.Length].number;
                            break;
                        case Giruda.Heart:
                            h.shape = Shape.Heart;
                            h.number = heart[heart.Length].number;
                            break;
                        case Giruda.Spade:
                            h.shape = Shape.Spade;
                            h.number = spade[spade.Length].number;
                            break;
                        case Giruda.NoGiruda:
                            h = MinCard();
                            break;
                    }
                    return h;
                }
            }
                
            if (gameInfo.roundIdx != 0 && gameInfo.roundIdx != 1)
            {
                Random r = new Random();
                if (lastRoundWinner != gameInfo.myInfo.position)
                {

                    firstCard.shape = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0].shape;
                    if (FindShape(firstCard) > 0)
                    {
                        switch (firstCard.shape)
                        {
                            case Shape.Clover:
                                do
                                {
                                    n = r.Next(clover_count);
                                } while (clover[n].isUsed == true);
                                cn = clover[n].number;
                                break;
                            case Shape.Diamond:
                                do
                                {
                                    n = r.Next(diamond_count);
                                } while (diamond[n].isUsed == true);
                                cn = diamond[n].number;
                                break;
                            case Shape.Heart:
                                do
                                {
                                    n = r.Next(heart_count);
                                } while (heart[n].isUsed == true);
                                cn = heart[n].number;
                                break;
                            case Shape.Spade:
                                do
                                {
                                    n = r.Next(spade_count);
                                } while (spade[n].isUsed == true);
                                cn = spade[n].number;
                                break;
                            default:
                                break;
                        }

                        h.shape = firstCard.shape;
                        h.number = cn;
                        return h;
                    }
                    else
                    {
                        if (FindShape(gCard) > 0)
                        {
                            switch (gCard.shape)
                            {
                                case Shape.Clover:
                                    do
                                    {
                                        n = r.Next(clover_count);
                                    } while (clover[n].isUsed == true);
                                    cn = clover[n].number;
                                    break;
                                case Shape.Diamond:
                                    do
                                    {
                                        n = r.Next(diamond_count);
                                    } while (diamond[n].isUsed == true);
                                    cn = diamond[n].number;
                                    break;
                                case Shape.Heart:
                                    do
                                    {
                                        n = r.Next(heart_count);
                                    } while (heart[n].isUsed == true);
                                    cn = heart[n].number;
                                    break;
                                case Shape.Spade:
                                    do
                                    {
                                        n = r.Next(spade_count);
                                    } while (spade[n].isUsed == true);
                                    cn = spade[n].number;
                                    break;
                                default:
                                    break;
                            }

                            h.shape = gCard.shape;
                            h.number = cn;
                            return h;
                        }
                        else
                        {
                            h = MinCard();
                            return h;
                        }
                    }

                }

                else if (joker.Length > 0 && joker[0].isUsed == false && gameInfo.myInfo.position == lastRoundWinner)
                {

                    joker[0].cmdJokerTarget = (Shape)gameGiruda;
                    return joker[0];
                }



                else if (FindShape(gCard) > 0)
                {
                    switch (gCard.shape)
                    {
                        case Shape.Clover:
                            do
                            {
                                n = r.Next(clover_count);
                            } while (clover[n].isUsed == true);
                            cn = clover[n].number;
                            break;
                        case Shape.Diamond:
                            do
                            {
                                n = r.Next(diamond_count);
                            } while (diamond[n].isUsed == true);
                            cn = diamond[n].number;
                            break;
                        case Shape.Heart:
                            do
                            {
                                n = r.Next(heart_count);
                            } while (heart[n].isUsed == true);
                            cn = heart[n].number;
                            break;
                        case Shape.Spade:
                            do
                            {
                                n = r.Next(spade_count);
                            } while (spade[n].isUsed == true);
                            cn = spade[n].number;
                            break;
                        default:
                            break;
                    }
                    h.shape = (Shape)gameGiruda;
                    h.number = cn;
                    return h;
                }

                else
                {
                    h = MinCard();
                    return h;
                }

            }

            else
            {
                h = MinCard();
                return h;

            }

        }

      /*  public HandedCard nowFriend()
        {
            HandedCard h = new HandedCard();
            switch (gameGiruda)
            {
                case Giruda.Diamond:
                    if (diamond.Length <= 0) return MinCard();
                    h.shape = diamond[0].shape;
                    h.number = diamond[0].number;
                    return h;
                case Giruda.Spade:
                    if (spade.Length <= 0) return MinCard();
                    h.shape = spade[0].shape;
                    h.number = spade[0].number;
                    return h;
                case Giruda.Heart:
                    if (heart.Length <= 0) return MinCard();
                    h.shape = heart[0].shape;
                    h.number = heart[0].number;
                    return h;
                case Giruda.Clover:
                    if (clover.Length <= 0) return MinCard();
                    h.shape = clover[0].shape;
                    h.number = clover[0].number;
                    return h;
                default:
                    h = MinCard();
                    return h;
            }

        }*/

        public HandedCard nowDefense()
        {
            Card ace = new Card();
            ace.number = 14;
            int a=0;
            HandedCard h = new HandedCard();

            if (gameInfo.roundIdx == 0)
            {
                switch (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0].shape)
                {
                    case Shape.Clover:
                        if (clover.Length > 0)
                        {
                            h.number = clover[0].number;
                            h.shape = Shape.Clover;
                            return h;
                        }
                        else
                        {
                            switch (gameGiruda)
                            {
                                case Giruda.Diamond:
                                    if (diamond.Length > 0)
                                    {
                                        h.number = diamond[0].number;
                                        h.shape = Shape.Diamond;
                                        return h;
                                    } break;
                                case Giruda.Heart:
                                    if (heart.Length > 0)
                                    {
                                        h.number = heart[0].number;
                                        h.shape = Shape.Heart;
                                        return h;
                                    } break;
                                case Giruda.Spade:
                                    if (spade.Length > 0)
                                    {
                                        h.number = spade[0].number;
                                        h.shape = Shape.Spade;
                                        return h;
                                    } break;
                                case Giruda.NoGiruda:
                                    h=MinCard();
                                    return h;
                            }
                        } break;
                    case Shape.Diamond:
                        if (diamond.Length > 0)
                        {
                            h.number = diamond[0].number;
                            h.shape = Shape.Diamond;
                            return h;
                        }
                        else
                        {
                            switch (gameInfo.betting.giruda)
                            {
                                case Giruda.Clover:
                                    if (clover.Length > 0)
                                    {
                                        h.number = clover[0].number;
                                        h.shape = Shape.Clover;
                                        return h;
                                    } break;
                                case Giruda.Heart:
                                    if (heart.Length > 0)
                                    {
                                        h.number = heart[0].number;
                                        h.shape = Shape.Heart;
                                        return h;
                                    } break;
                                case Giruda.Spade:
                                    if (spade.Length > 0)
                                    {
                                        h.number = spade[0].number;
                                        h.shape = Shape.Spade;
                                        return h;
                                    } break;
                                case Giruda.NoGiruda:
                                    h=MinCard();
                                    return h;
                            }
                        } break;

                    case Shape.Heart:
                        if (heart.Length > 0)
                        {
                            h.number = heart[0].number;
                            h.shape = Shape.Heart;
                            return h;
                        }
                        else
                        {
                            switch (gameInfo.betting.giruda)
                            {
                                case Giruda.Diamond:
                                    if (diamond.Length > 0)
                                    {
                                        h.number = diamond[0].number;
                                        h.shape = Shape.Diamond;
                                        return h;
                                    } break;
                                case Giruda.Clover:
                                    if (clover.Length > 0)
                                    {
                                        h.number = clover[0].number;
                                        h.shape = Shape.Clover;
                                        return h;
                                    } break;
                                case Giruda.Spade:
                                    if (spade.Length > 0)
                                    {
                                        h.number = spade[0].number;
                                        h.shape = Shape.Spade;
                                        return h;
                                    } break;
                                case Giruda.NoGiruda:
                                    h=MinCard();
                                    return h;
                            }
                        }break;

                    case Shape.Spade:
                        if (spade.Length > 0)
                        {
                            h.number = spade[0].number;
                            h.shape = Shape.Spade;
                            return h;
                        }
                        else
                        {
                            switch (gameInfo.betting.giruda)
                            {
                                case Giruda.Diamond:
                                    if (diamond.Length > 0)
                                    {
                                        h.number = diamond[0].number;
                                        h.shape = Shape.Diamond;
                                        return h;
                                    } break;
                                case Giruda.Heart:
                                    if (heart.Length > 0)
                                    {
                                        h.number = heart[0].number;
                                        h.shape = Shape.Heart;
                                        return h;
                                    } break;
                                case Giruda.Clover:
                                    if (clover.Length > 0)
                                    {
                                        h.number = clover[0].number;
                                        h.shape = Shape.Clover;
                                        return h;
                                    } break;
                                case Giruda.NoGiruda:
                                    h=MinCard();
                                    return h;

                            }
                        } break;
                }
            }

            //마이티 끌어내기
            if (gameInfo.roundHistory[gameInfo.roundIdx - 1].winner == gameInfo.myInfo.position)
            {
                if (gameGiruda == Giruda.Spade)
                {
                    if (diamond.Length > 0)
                    {
                        h.shape = Shape.Diamond;
                        h.number = diamond[0].number;
                        return h;
                    }
                    else if (clover[clover.Length].number == 14)
                    {
                        h.shape = Shape.Clover;
                        h.number = 14;
                        return h;
                    }
                    else if (heart[heart.Length].number == 14)
                    {
                        h.shape = Shape.Heart;
                        h.number = 14;
                        return h;
                    }
                    else
                    {
                        h = MinCard();
                        return h;
                    }
                }

                else
                {
                    if (spade.Length > 0)
                    {
                        h.number = spade[0].number;
                        h.shape = Shape.Spade;
                        return h;
                    }
                    switch (gameGiruda)
                    {
                        case Giruda.Clover:
                            if (diamond[diamond.Length].number == 14)
                            {
                                h.shape = Shape.Diamond;
                                h.number = 14;
                                return h;
                            }
                            else if (heart[heart.Length].number == 14)
                            {
                                h.shape = Shape.Heart;
                                h.number = 14;
                                return h;
                            }
                            else
                            {
                                h=MinCard();
                                return h;
                            }
                        case Giruda.Diamond:
                            if (clover[clover.Length].number == 14)
                            {
                                h.shape = Shape.Clover;
                                h.number = 14;
                                return h;
                            }
                            else if (heart[heart.Length].number == 14)
                            {
                                h.shape = Shape.Heart;
                                h.number = 14;
                                return h;
                            }
                            else
                            {
                               h= MinCard();
                               return h;
                            } 
                        case Giruda.Heart:
                            if (clover[clover.Length].number == 14)
                            {
                                h.shape = Shape.Clover;
                                h.number = 14;
                                return h;
                            }
                            else if (diamond[diamond.Length].number == 14)
                            {
                                h.shape = Shape.Diamond;
                                h.number = 14;
                                return h;
                            }
                            else
                            {
                                h=MinCard();
                                return h;
                            } 
                    }
                                
                            
                 }
                 h=MinCard();
                 return h;
            }

            if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0].isJokerCall && joker.Length == 1)
            {
                h.shape = Shape.Joker;
                return h;
            }

            else if (joker.Length == 1 && joker[0].isUsed == false)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i] != null
                        &&gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].number>9)
                        a += gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].number;
                }

                if (a > 20)
                {
                    h.shape = Shape.Joker;
                    return h;
                }
            }

            switch (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0].shape)
            {
                case Shape.Diamond:
                    if (diamond.Length <= 0) return MinCard();
                    h.shape = diamond[0].shape;
                    h.number = diamond[0].number;
                    return h;
                case Shape.Spade:
                    if (spade.Length <= 0) return MinCard();
                    h.shape = spade[0].shape;
                    h.number = spade[0].number;
                    return h;
                case Shape.Heart:
                    if (heart.Length <= 0) return MinCard();
                    h.shape = heart[0].shape;
                    h.number = heart[0].number;
                    return h;
                case Shape.Clover:
                    if (clover.Length <= 0) return MinCard();
                    h.shape = clover[0].shape;
                    h.number = clover[0].number;
                    return h;
                default:
                    h = MinCard();
                    return h;
            }

        }
    }
}